﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.WarehouseManagement
{
    public class OutsourcingMaterialService:ApplicationService,OutsourcingMaterialIService
    {
        private readonly IRepository<OutsourcingMaterial, int> _outsourcingrepository;
        public OutsourcingMaterialService(IRepository<OutsourcingMaterial, int> outsourcingrepository)
        {
            _outsourcingrepository = outsourcingrepository;
        }

        public async Task<int> AddOutsourcingMaterial(OutsourcingMaterialDTO outsourcingMaterial)
        {
            var list = ObjectMapper.Map<OutsourcingMaterialDTO, OutsourcingMaterial>(outsourcingMaterial);
            var result = await _outsourcingrepository.InsertAsync(list);
            return result == null ? 0 : 1;
        }

        public async Task<int> DeleteOutsourcingMaterial(int outsourcingMaterialId)
        {
            var Del = _outsourcingrepository.DeleteAsync(c => c.Id == outsourcingMaterialId);
            return Del == null ? 0 : 1;
        }

        public async Task<OutsourcingMaterialDTO> FanOutsourcingMaterial(int outsourcingMaterialId)
        {
            var list = await _outsourcingrepository.FindAsync(c => c.Id == outsourcingMaterialId);
            var result = ObjectMapper.Map<OutsourcingMaterial, OutsourcingMaterialDTO>(list);
            return result;
        }

        public async Task<List<OutsourcingMaterialDTO>> GetOutsourcingMaterial(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _outsourcingrepository.GetListAsync();
            var result = ObjectMapper.Map<List<OutsourcingMaterial>, List<OutsourcingMaterialDTO>>(list);
            return result;
        }

        public async Task<int> UpdateOutsourcingMaterial(OutsourcingMaterialDTO outsourcingMaterial)
        {
            var list = ObjectMapper.Map<OutsourcingMaterialDTO, OutsourcingMaterial>(outsourcingMaterial);
            var result = await _outsourcingrepository.UpdateAsync(list);
            return result == null ? 0 : 1;
        }
    }
}
